/**
 * Created with JetBrains WebStorm.
 * User: chamara
 * Date: 8/3/13
 * Time: 5:43 PM
 * To change this template use File | Settings | File Templates.
 */

describe("PublicLoginController", function(){
    var controller;

    beforeEach(function() {
        App.PublicLoginController.reopen({
            init: function() {
            }
        });

        controller = App.PublicLoginController.create({
            controllers: Ember.Object.create({
                alert: Ember.Object.create({
                    showAlert: function(msg) {
                    }
                })
            })
        });
    })

    describe('verifyInputs', function() {
        beforeEach(function(){
            controller = App.PublicLoginController.create();
        });

        it('empty username', function() {
            spyOn(BreezeException, 'create');
            try {
                controller.verifyInputs('', 'password', '');
            } catch(e) {
            }
            expect(BreezeException.create).toHaveBeenCalledWith({errorMessage: controller.get('i18nError.ERR_SEC_USER_ID_REQUIRED')});
        });
        it('empty password', function() {
            spyOn(BreezeException, 'create');
            try {
                controller.verifyInputs('username', '', '');
            } catch(e) {
            }
            expect(BreezeException.create).toHaveBeenCalledWith({errorMessage: controller.get('i18nError.ERR_SEC_PASSWORD_REQUIRED')});
        });
        it('empty username & password', function() {
            spyOn(BreezeException, 'create');
            try {
                controller.verifyInputs('', '', '');
            } catch(e) {
            }
            expect(BreezeException.create).toHaveBeenCalledWith({errorMessage: controller.get('i18nError.ERR_SEC_USER_ID_PASSWORD_REQUIRED')});
        });
        it('valid username and password', function() {
            spyOn(BreezeException, 'create');
            controller.verifyInputs('username', 'password', '');
            expect(BreezeException.create).not.toHaveBeenCalled();
        });
    });
    describe('submitLogin', function() {
        it('should display error when there is validation error', function() {
            spyOn(controller, 'verifyInputs').andThrow({getErrorMessage: function() {return 'error'}});
            spyOn(controller, '_clearInput');
            spyOn(controller.get('controllers').get('alert'), 'showAlert');
            spyOn(controller, 'showLoading');
            spyOn(App.api_user, 'login');

            controller.submitLogin();

            expect(controller._clearInput).toHaveBeenCalled();
            expect(controller.get('controllers').get('alert').showAlert).toHaveBeenCalledWith('error');
            expect(controller.showLoading).not.toHaveBeenCalled();
            expect(App.api_user.login).not.toHaveBeenCalled();
        });
        it('should call webservice when validation is successful', function() {
            spyOn(controller, 'verifyInputs').andReturn(true);
            spyOn(controller, '_clearInput');
            spyOn(controller.get('controllers').get('alert'), 'showAlert');
            spyOn(controller, 'showLoading');
            spyOn(controller, 'callback');
            spyOn(App.api_user, 'login');

            controller.submitLogin();

            expect(controller._clearInput).not.toHaveBeenCalled();
            expect(controller.get('controllers').get('alert').showAlert).not.toHaveBeenCalled();
            expect(controller.showLoading).toHaveBeenCalled();
            expect(App.api_user.login).toHaveBeenCalled();
//            expect(controller.callback).toHaveBeenCalled();
        });
    });

    describe("callback method test", function(){

        it("callback method - ", function(){

            var objE = {errorCode:'code', errorMessage:'message'};

            spyOn(controller, 'hideLoading');
            spyOn(controller, '_clearInput');
            spyOn(App, 'startSessionTimer');
            spyOn(controller, 'hasError').andReturn(false);
            spyOn(controller, 'transitionTo');

            controller.callback(objE);

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(controller._clearInput).toHaveBeenCalled();
            expect(App.startSessionTimer).toHaveBeenCalled();
            expect(controller.hasError).toHaveBeenCalledWith(objE);
            expect(controller.transitionTo).toHaveBeenCalledWith('see');
        });
    });

    describe("_clearInput method test", function(){

        it("_clearInput method - ", function(){

            spyOn(controller, 'set').andReturn(null);
            controller._clearInput();
            expect(controller.set).toHaveBeenCalledWith('username', null);
            expect(controller.set).toHaveBeenCalledWith('password', null);
            expect(controller.set).toHaveBeenCalledWith('captcha', null);
        });
    });
});